package com.lc.recovery.ui.activity;

import android.os.Bundle;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.recovery.R;
import com.lc.recovery.app.AppActivity;
import com.lc.recovery.http.request.EditApi;
import com.lc.recovery.http.response.PublicMsg;
import com.lc.recovery.other.IntentKey;
import com.lc.recovery.util.SPUtil;
import com.lc.widget.view.PasswordEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PasswordEditActivity extends AppActivity {
    @BindView(R.id.et_password_forget_phone)
    PasswordEditText etPasswordForgetPhone;
    @BindView(R.id.et_password_forget_newphone)
    PasswordEditText etPasswordForgetNewphone;
    @BindView(R.id.et_password_forget_truephone)
    PasswordEditText etPasswordForgetTruephone;
    private String user_type;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_passwordedit;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.btn_password_forget_commit)
    public void onClick() {
        hideKeyboard(getCurrentFocus());
        if (etPasswordForgetPhone.getText().toString().isEmpty()){
            toast("旧密码不能为空");
            return;
        }
        if (etPasswordForgetNewphone.getText().toString().isEmpty()){
            toast("新密码不能为空");
            return;
        }

        if (etPasswordForgetTruephone.getText().toString().isEmpty()){
            toast("确认密码不能为空");
            return;
        }
        if (etPasswordForgetNewphone.getText().toString().length()<6||etPasswordForgetNewphone.getText().toString().length()>20||etPasswordForgetTruephone.getText().toString().length()<6||etPasswordForgetTruephone.getText().toString().length()>20){
            toast("密码需要6-20个字符");
            return;
        }
        if (!etPasswordForgetTruephone.getText().toString().equals(etPasswordForgetNewphone.getText().toString())){
            toast("两次输入密码不一致");
            return;
        }
        if (SPUtil.get(this, IntentKey.TYPE,"").equals("1")||SPUtil.get(this, IntentKey.TYPE,"").equals("2")){
            user_type = "1";
        }else if (SPUtil.get(this, IntentKey.TYPE,"").equals("3")){
            user_type="2";
        }else {
            user_type="3";
        }
        edite();
    }
    private void edite() {
        EasyHttp.post(this)
                .api(new EditApi()
                        .setOld_password(etPasswordForgetPhone.getText().toString()).setPassword(etPasswordForgetNewphone.getText().toString()).setType(user_type).setUser_id(SPUtil.get(this, IntentKey.USERID,"")))
                .request(new HttpCallback<PublicMsg>(this) {

                    @Override
                    public void onSucceed(PublicMsg data) {
                        if (data.getCode()==200){
                            toast(data.getMsg());
                            finish();
                        }else {
                            toast(data.getMsg());
                        }
                    }
                });
    }
}
